import plotly.graph_objects as go
from pymongo import MongoClient
import sys

dbName = sys.argv[1]

# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017')
db = client['cp-test']
# 1k-1m
# collection = db['cp_record_4']
#200-1k
# collection = db['cp_record_5']
collection = db[dbName]

# Retrieve the data
data = collection.find().sort('lengthSource', 1)

# Initialize empty dictionaries for each type
se_rate_data = {}
cp_rate_data = {}
all_rate_data = {}
cost_time_cp_data = {}
cost_time_dcp_data = {}
cost_time_se_data = {}
cost_time_dse_data = {}
cost_time_all1_data = {}
cost_time_all2_data = {}

# Iterate over the retrieved data
for document in data:
    # Extract the relevant fields from the document
    source_size = document['lengthSe']
    info_lenth = document['lengthSource']
    cp_rate = document['cpRate']/10000
    se_rate = document['seRate']/10000
    cost_time_cp = document['costTimeCp']
    cost_time_dcp = document['costTimeDcp']
    cost_time_se = document['costTimeSe']
    cost_time_dse = document['costTimeDse']
    cost_time_all1 = document['costTimeSe'] + document['costTimeCp']
    cost_time_all2 = document['costTimeDse'] + document['costTimeDcp']
    type = document['channelType'] +' : '+ document['type']
    all_rate = document['lengthCp'] * 100 / document['lengthSource'] 






    # Append the data to the respective dictionaries based on the type
    se_rate_data.setdefault(type, []).append((info_lenth, se_rate))
    cp_rate_data.setdefault(type, []).append((info_lenth, cp_rate))
    all_rate_data.setdefault(type, []).append((info_lenth, all_rate))
    cost_time_cp_data.setdefault(type, []).append((info_lenth, cost_time_cp))
    cost_time_dcp_data.setdefault(type, []).append((info_lenth, cost_time_dcp))
    cost_time_se_data.setdefault(type, []).append((info_lenth, cost_time_se))
    cost_time_dse_data.setdefault(type, []).append((info_lenth, cost_time_dse))
    cost_time_all1_data.setdefault(type, []).append((info_lenth, cost_time_all1))
    cost_time_all2_data.setdefault(type, []).append((info_lenth, cost_time_all2))

def openView(title,xaxis_title,yaxis_title,items):
    fig = go.Figure()
    for type, data_points in items:
        x, y = zip(*data_points)
        fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name=type))
    fig.update_layout(
        title=title,
        xaxis_title=xaxis_title,
        yaxis_title=yaxis_title,
        hovermode='closest',
        hoverlabel=dict(namelength=-1),
    )
    # fig.update_traces(text=[type for _ in range(len(items))])
    fig.show()

# openView('压缩率','Source Size','Integer cpRate',cp_rate_data.items())
# openView('压缩时间','Source Size','Compression Time',cost_time_cp_data.items())
# openView('解压时间','Source Size','Decompression Time',cost_time_dcp_data.items())

# openView('序列化比例','Source Size','Integer seRate',se_rate_data.items())
# openView('序列化时间','Source Size','SE Time',cost_time_se_data.items())
# openView('反序列化时间','Source Size','Decompression Time',cost_time_dse_data.items())

openView('总压缩比例','Source Size','Integer allRate',all_rate_data.items())
openView('总序列化压缩时间','Source Size','All Time',cost_time_all1_data.items())
openView('总反序列化解压时间','Source Size','All Time',cost_time_all2_data.items())
    
